"""
自定义文件传送相关封装
"""
import paramiko


def sftp_stream_copy(src_path, dst_host, dst_user, dst_pass, dst_path, bufsize=32768):
    src_host = "192.168.17.51"
    src_port = 22
    src_user = "root"
    src_pass = "111"
    dst_port = 22

    # 连接源服务器
    src_transport = paramiko.Transport((src_host, src_port))
    src_transport.connect(username=src_user, password=src_pass)
    src_sftp = paramiko.SFTPClient.from_transport(src_transport)

    # 连接目标服务器
    dst_transport = paramiko.Transport((dst_host, dst_port))
    dst_transport.connect(username=dst_user, password=dst_pass)
    dst_sftp = paramiko.SFTPClient.from_transport(dst_transport)

    # 打开源文件和目标文件
    with src_sftp.open(src_path, "rb") as src_file, \
         dst_sftp.open(dst_path, "wb") as dst_file:

        while True:
            data = src_file.read(bufsize)
            if not data:
                break
            dst_file.write(data)

    # 关闭连接
    src_sftp.close()
    src_transport.close()
    dst_sftp.close()
    dst_transport.close()


if __name__ == "__main__":
    sftp_stream_copy(
        src_path="/mnt/dev_sdb/paauto/tools/test0926.sh",
        dst_host="192.169.103.1", dst_user="root", dst_pass="100trust.", dst_path="/root/test0926.sh"
    )
